/**********************************************************************************
// Ship
// 
// Creation:	06 Mai 2011
// Updated:		02 Jun 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Defines a space ship for the game Space Race
//
**********************************************************************************/

#include "Ship.h"

/**********************************************************************************/

Ship::Ship() : Sprite("Resources/SpaceShip.png", 43, 60, 6)
{
	speed.SetVector(90.0f, 0.0f);		// starts upward and still
	direction.SetVector(90.0f, 0.0f);	// starts upward and still
	customType = objShip;

	// create space ship bounding box
	Point shipVertex[4] = {Point(0,-30), Point(21,17), Point(0,15), Point(-21,17)};
	boundingbox = new Poly(shipVertex, 4);
	boundingbox->color = Color(255,255,0);
	boundingbox->customType = objShip;

	uint seqInertia[]    = {0};
	uint seqForwThrust[] = {1,2,3,4};
	uint seqBackThrust[] = {5};

	// define space ship animations
	inertialShipAnim = new Animation(seqInertia, 1, 0, true);
	forwardThrustAnim = new Animation(seqForwThrust, 4, 60, true);
	backwadThrustAnim = new Animation(seqBackThrust, 1, 0, true);
	
	shipExplosion = new Sprite("Resources/SpaceShipExplosion.png", 84, 84, 4);
	shipExplosionAnim = nullptr;
}

/**********************************************************************************/

Ship::~Ship()
{
	delete boundingbox;
	delete inertialShipAnim;
	delete forwardThrustAnim;
	delete backwadThrustAnim;
	delete shipExplosion;
}

/**********************************************************************************/

void Ship::Rotate(float ang)
{
	Sprite::Rotate(ang);
	direction.Rotate(ang);
}

/**********************************************************************************/

void Ship::Accelerate()
{
	direction.SetMagnitude(0.50f);
	speed.Add(direction);
}

/**********************************************************************************/

void Ship::Brake()
{
	direction.SetMagnitude(-0.20f);
	speed.Add(direction);
}

/**********************************************************************************/

void Ship::Reset()
{
	// reset bounding box to original non-transformed vertices
	Point shipVertex[4] = {Point(0,-30), Point(21,17), Point(0,15), Point(-21,17)};
	((Poly*) boundingbox)->SetVertices(shipVertex, 4);

	// reset sprite rotation
	rotation = 0;
	
	// reset speed and direction
	speed.SetVector(90.0f, 0.0f);
	direction.SetVector(90.0f, 0.0f);
}

/**********************************************************************************/
// change ship animation sequence

void Ship::Animate(int anim)
{
	switch (anim)
	{
	case INERTIA: animation = inertialShipAnim; break;
	case FORWARD: animation = forwardThrustAnim; break;
	case BACKWARD: animation = backwadThrustAnim; break;
	default: animation = nullptr;
	}
}

/**********************************************************************************/
// play space ship explosion animation

void Ship::Explode()
{
	shipExplosionAnim = new Animation(nullptr, 8, 33, false, x, y);
	shipExplosion->animation = shipExplosionAnim;
	shipExplosion->rotation = rotation;
}

/**********************************************************************************/
// update space ship explosion animation

bool Ship::isExploding()
{
	if (shipExplosion->animation)
		return true;
	else
		return false;
}


/**********************************************************************************/
// update space ship explosion animation

void Ship::DrawExplosion()
{
	if (shipExplosion->animation->Active())
	{
		shipExplosion->animation->NextFrame();
		shipExplosion->animation->xPos += (speed.GetX()/16);
		shipExplosion->animation->yPos -= (speed.GetY()/16);
		shipExplosion->Draw();
	}
	else
	{
		delete shipExplosion->animation;
		shipExplosion->animation = nullptr;
	} 
}

/**********************************************************************************/